import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { Observable } from 'rxjs';
import { AxiosResponse } from 'axios';

export interface ConnectAuthResponce {
  access_token: string;
  exoires_in: number;
  token_type: string;
  scope: string;
}

@Injectable()
export class MDMAuthService {
  constructor(private httpService: HttpService) {}

  auth(): Observable<AxiosResponse<ConnectAuthResponce>> {
    const params = {
      grant_type: 'client_credentials',
      client_id: process.env.MDM_CLIENT_ID,
      client_secret: process.env.MDM_CLIENT_SECRET,
    };

    return this.httpService.post(
      `${process.env.MDM_URL}/connect/token`,
      params,
      {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      },
    );
  }
}
